#!/usr/bin/make -f

BUILD_OUTPUT ?= ./out
brunch ?= master
APP_NAME ?= app
APP_EXAMPLE_DIR = ""

build-payment:
	CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o $(BUILD_OUTPUT)/payment ./payment/

docker-build-payment:
	$(eval APP_NAME=smart-payment)
	$(eval APP_EXAMPLE_DIR=example/payment)
	@echo "build payment docker image with tag $(brunch). APP_NAME=$(APP_NAME) APP_EXAMPLE_DIR=$(APP_EXAMPLE_DIR)"
	@sudo rm -fr $(APP_EXAMPLE_DIR)
	sudo docker build --no-cache --build-arg VERSION=$(brunch) -t $(APP_NAME):$(brunch) -f Dockerfile.payment.dockerfile .
	@mkdir -p $(APP_EXAMPLE_DIR)
	@cp -f payment/*.yaml $(APP_EXAMPLE_DIR)/
	@sed -i 's/APP_NAME/$(APP_NAME)/g' $(APP_EXAMPLE_DIR)/docker-compose.yaml
	@sed -i 's/VERSION/$(brunch)/g' $(APP_EXAMPLE_DIR)/docker-compose.yaml

docker-build-payment-local:
	$(eval APP_NAME=smart-payment)
	$(eval APP_EXAMPLE_DIR=example/payment)
	@echo "build payment docker image with tag $(brunch). APP_NAME=$(APP_NAME) APP_EXAMPLE_DIR=$(APP_EXAMPLE_DIR)"
	@sudo rm -fr $(APP_EXAMPLE_DIR)
	sudo docker build --no-cache --build-arg VERSION=$(brunch) -t $(APP_NAME):$(brunch) -f Dockerfile.payment.local.dockerfile .
	@mkdir -p $(APP_EXAMPLE_DIR)
	@cp -f payment/*.yaml $(APP_EXAMPLE_DIR)/
	@sed -i 's/APP_NAME/$(APP_NAME)/g' $(APP_EXAMPLE_DIR)/docker-compose.yaml
	@sed -i 's/VERSION/$(brunch)/g' $(APP_EXAMPLE_DIR)/docker-compose.yaml

clean:
	rm -fr $(BUILD_OUTPUT)


help:
	@echo "可用命令:"
	@echo "  make build-payment           							- 本地构建payment应用"
	@echo "  make docker-build-payment brunch=latest   				- 构建payment Docker镜像"
	@echo "  make docker-build-payment-local brunch=v0.0.1   		- 使用本地构建 payment 制作 Docker镜像"
	@echo "  make clean           									- 清理本地构建产物"
	@echo "  make help            									- 显示帮助信息"
